package com.arvato.wxwork.controller.admin.staff;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.staff.WxworkPositionDo;
import com.arvato.wxwork.bean.bean_vo.staff.WxworkPositionVo;
import com.arvato.wxwork.service.staff.WxworkPositionService;


/**
 * 企业微信门店管理Controller
 * @author zhan655
 *
 */
@Controller
@RequestMapping("/admin/wxwork/position/")
public class WxworkPositionController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkPositionController.class);

	@Autowired
	private WxworkPositionService wxworkPositionService;
	
	/**
	 * 获取企业微信员工列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData listPosition(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("获取员工列表");
		PageData pageData = wxworkPositionService.listPositionByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 获取一个员工的详情
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result positionDetail(String positionId) {
		WxworkPositionVo positionVo = wxworkPositionService.getPositionById(positionId);
		return Result.success(positionVo);
	}
	
	/**
	 * 创建职位
	 * @param req
	 * @param positionDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result create(HttpServletRequest req, WxworkPositionDo positionDo) {
		logger.info("新增一个职位");
		try {
			wxworkPositionService.insert(positionDo);
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,职位编号:" + positionDo.getPositionCode() + "已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("创建职位失败：" + e.getMessage());
		}
		return Result.success("创建职位成功。");
	}
	
	/**
	 * 更新职位
	 * @param req
	 * @param positionDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(HttpServletRequest req, WxworkPositionDo positionDo) {
		logger.info("更新一个职位");
		try {
			wxworkPositionService.update(positionDo);
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,职位编号:" + positionDo.getPositionCode() + "已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新职位失败：" + e.getMessage());
		}
		return Result.success("更新职位成功。");
	}
	
	/**
	 * 删除职位
	 * @param req
	 * @param positionId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(HttpServletRequest req, String positionId) {
		logger.info("更新一个职位节点");
		try {
			wxworkPositionService.delete(positionId);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新职位失败：" + e.getMessage());
		}
		return Result.success("更新职位成功。");
	}
	
}

